<?php
// +----------------------------------------------------------------------
// | Author: derek <693472824@qq.com> <http://www.bloveambition.com/>
// +----------------------------------------------------------------------

namespace Blovex4\Controller;
use Blovex4\Logic\AdminLogic;

/**
 * 订单控制器
 * @author derek <693472824@qq.com>
 */
class OrdersController extends BaseController {

    /**
     * 获取订单列表
     * @author derek <693472824@qq.com>
     */
    public function index () {
        
        $map = array();

        $data = I();            //取所有查询条件
        
        //1：新下订单；2:已有小哥待服务；3：小哥正在服务；4：服务结束待收费；5：用户现金付款待小哥确认；6：已收费，订单结束；7：订单被取消
        if(!empty($data['status'])){
            $map['Orders.status'] = $data['status'];
        }
        
        if(!empty($data['account'])){
            $map['User.account'] = array('like', '%'.$data['account'].'%');
        }
        if(!empty($data['username'])){
            $map['User.username'] = array('like', '%'.$data['username'].'%');
        }
        if(!empty($data['contact'])){
            $map['UserAddress.contact'] = array('like', '%'.$data['contact'].'%');
        }
        if(!empty($data['telphone'])){
            $map['UserAddress.telphone'] = array('like', '%'.$data['telphone'].'%');
        }
        if(!empty($data['emergency'])){
            $map['Orders.emergency'] = $data['emergency'];
        }
        if(!empty($data['eid'])){
            $map['Worker.eid'] = array('like', '%'.$data['eid'].'%');
        }
        if(!empty($data['w_contact'])){
            $map['Worker.account'] = array('like', '%'.$data['w_contact'].'%');
        }
        if(!empty($data['order_no'])){
            $map['Orders.order_no'] = array('like', '%'.$data['order_no'].'%');
        }
        
        $siteStr = AdminLogic::getSite();
        
        if(!empty($siteStr)){
            $map['Orders.site_id'] = array('in', $siteStr);
            $siteMap['site_id'] = $siteStr;
        }elseif (!empty($data['site_id'])){
            
            $map['Orders.site_id'] = $data['site_id'];
            $siteMap = array();
        }
        
        $siteList = M('Site')->where($siteMap)->getField('site_id,site_name,parent_id');
        
        $view = D('OrdersView');
        $list = $this->lists($view, $map, 'Orders.status,Orders.emergency DESC,Orders.create_time DESC');
        
        $detailList = D('Class', 'Logic')->getDetailList();
        
        $this->assign('map', $data);
        $this->assign('detailList', $detailList);
        $this->assign('list', $list);
        $this->assign('siteList', $siteList);
        $this->assign('w_contact', $data['w_contact']);
        $this->display();
    }

    /**
     * 下单
     *
     * @param integer $admin_id
     *            用户ID.
     * @author derek <693472824@qq.com>
     */
    public function editAction() {
        
        $userID = I('user_id');
        
        if(!empty($userID)){
        
            $addressID = I('address_id');
            
            $map['user_id'] = $userID;
            $user = M('User')->where($map)->find();
            $addressList = M('UserAddress')->where($map)->select();
            
            $this->assign('userModel', $user);
            $this->assign('address_id', $addressID);
            $this->assign('list', $addressList);
        }
               
        $classList = M('Class')->where(array('parent_id'=>array('gt', '0')))->getField('class_id,class_name,profession');
        $detailList = M('ClassDetail')->select();
        
        foreach ($detailList as $key=>$val){
            
            $classList[$val['class_id']]['_child'][] = $val;
        }
        
        $this->assign('detailList', $classList);
        $this->display();
    }
    
    /**
     * 保存站点信息
     * @author derek <lixm@my818.com>
     */
    public function saveAction() {
    
        $orderLogic = D('Orders', 'Logic');
        $result = $orderLogic->saveAction();
        
        if($result === true){
            $this->success('操作成功', U('index'));
        }elseif ($result === '0'){
            $this->error('参数错误');
        }else{
            $this->error('操作失败');
        }
    }

    /**
     * 派单时获取自有团队成员，且需要小哥在订单的预约服务时间有空
     */
    public function memberList(){
        
        $result = array('status'=>0, 'msg'=>'', 'info'=>'');
        
        $orderID = I('order_id');
        
        if(empty($orderID)){
            $result['status'] = '0';
            $result['msg'] = '参数错误';
        }else{
        
            $list = D('Orders', 'Logic')->memberList($orderID);
            
            $result['status'] = '1';
            $result['info'] = $list;
        }
        
        $this->ajaxReturn($result);
    }
    
    /**
     * 后台派单
     */
    public function dispatch(){
        
        $result = array('status'=>0, 'msg'=>'', 'info'=>'');
        
        $orderID = I('order_id');
        $workerID = I('worker_id');
        
        if(empty($orderID) || empty($workerID)){
            $result['msg'] = '参数错误';
        }else{
            $orderLogic = D('Orders', 'Logic');
            $rs = $orderLogic->dispatch($orderID, $workerID);
            
            if($rs !== false){
                $result['status'] = '1';
                $result['msg'] = '操作成功';
            }else{
                $result['msg'] = '操作失败，请刷新后重试';
            }
        }
        
        $this->ajaxReturn($result);
    }
    
    /**
     * 校验用户账号是否存在，若存在则返回维修地址列表
     */
    public function validity(){
        
        $account = I('account');
        
        $map['account'] = $account;
        $user = M('User')->where($map)->find();
        
        if(!empty($user)){
            
            $list = M('UserAddress')->where($map)->select();
            
            $result['status'] = '1';
            $result['info'] = $list;
        }else{
            $result['status'] = '0';
        }
        
        $this->ajaxReturn($result);
    }
    
    /**
     * 查看小哥及其时间安排
     */
    public function queryWorker(){
        
        $eid = I('eid');
        
        $worker = M('Worker')->where(array('eid'=>$eid))->find();
        
        if(!empty($eid) && !empty($worker)){
            
            $result['status'] = $worker['worker_id'];
        }else{
            $result['status'] = '0';
        }
        
        $schedule = D('Api/Worker', 'Logic')->schedule($eid);
        $result['info'] = $schedule;
        
        $this->ajaxReturn($result);
    }
    
    /**
     * 取消订单
     */
    public function cancel(){
        
        $orderID = I('order_id');
        $contact = I('w_contact');
        
        $data['status'] = '7';
        $data['end_time'] = NOW_TIME;
        
        $result = M('Orders')->where(array('order_id'=>$orderID, 'status'=>array('in', '1,2,8')))->save($data);
        
        if($result){
            
            if(!empty($contact)){
                $url = U('Orders/index', array('w_contact'=>$contact));
            }else{
                $url = U('index');
            }
            
            $this->success('操作成功！', $url);
        }else{
            $this->error('操作失败');
        }
    }
    
    /**
     * 订单详情
     */
    public function viewAction(){
        
        $orderID = I('order_id');
        
        if(empty($orderID)){
            $this->error('参数错误', U('Index/index'));
        }
        
        $view = D('Api/OrdersView');
        
        $order = $view->where(array('order_id'=>$orderID))->find();
        $user = M('User')->where(array('user_id'=>$order['user_id']))->find();
        $sender = M('Worker')->where(array('worker_id'=>$order['sender']))->find();
        $worker = M('Worker')->where(array('worker_id'=>$order['worker_id']))->find();
        $priceList = M('OrdersPrice')->where(array('order_id'=>$orderID))->select();
        $recordList = M('OrdersRecord')->where(array('order_id'=>$orderID))->select();
        
        $this->assign('orderModel', $order);
        $this->assign('userModel', $user);
        $this->assign('senderModel', $sender);
        $this->assign('workerModel', $worker);
        $this->assign('list', $priceList);
        $this->assign('recordList', $recordList);
        $this->display();
    }
    
}